import pyglet
from pyglet.gl import *

window = pyglet.window.Window()

image = pyglet.image.load('../resources/stars.jpg')
mask = pyglet.image.load('../resources/light_classic.png')
texture = pyglet.image.Texture.create(image.width, image.height)

done = False

@window.event
def on_draw():
    window.clear()
    
    global done
    if not done:
        id = GLuint(0)
        glGenFramebuffersEXT(1, byref(id))
        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, id)
        glFramebufferTexture2DEXT(
            GL_FRAMEBUFFER_EXT,
            GL_COLOR_ATTACHMENT0_EXT,
            texture.target,
            texture.id,
            texture.level)

        glClear(GL_COLOR_BUFFER_BIT)
        glEnable(GL_BLEND)
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
        image.blit(100, 100)
        glBlendFunc(GL_ZERO, GL_SRC_ALPHA)
        mask.blit(0, 0)
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0)
        done = True
    
    glClearColor(0.8, 0.8, 0.8, 1)
    #texture.blit(40, 30)
    texture.blit(0, 0)

pyglet.app.run()

'''
import pyglet
from pyglet.gl import *
from random import *


window = pyglet.window.Window()
image = pyglet.image.load('../resources/stars.jpg') 
mask = pyglet.image.load('../resources/light.png')
createdtex=False;
imagetex = 0


@window.event
def on_draw():
    window.clear()
    global createdtex
    texfrmbuf =(GLuint*1)()
    global imagetex
    if createdtex!=True:
        imagetex = pyglet.image.Texture.create(image.width, image.height,GL_RGBA)
        glEnable(GL_BLEND)
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
        glGenFramebuffersEXT(1,texfrmbuf)
        glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT,texfrmbuf[0])
        glFramebufferTexture2DEXT(GL_DRAW_FRAMEBUFFER_EXT,GL_COLOR_ATTACHMENT0_EXT, imagetex.target,imagetex.id,0)
        for i in range(20):
            mask.blit(randint(0,image.width/5),randint(0,image.height/5))

        glFlush()
        glDisable(GL_BLEND)
        glDeleteFramebuffersEXT(1,texfrmbuf)
        glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT,0)
        createdtex=True

    image.blit(0,0)
    glEnable(GL_BLEND)
    glBlendFunc(GL_DST_COLOR, GL_ZERO)
    imagetex.blit(0,0)
    glDisable(GL_BLEND)

pyglet.app.run()
'''


